home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / CollationKey.java < prev    next >
Text File  |  1998-09-22  |  7KB  |  184 lines

  1. /*
  2.  * @(#)CollationKey.java    1.4 97/01/28
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33. /**
  34.  * A <code>CollationKey</code> represents a <code>String</code> under the
  35.  * rules of a specific <code>Collator</code> object. Comparing two
  36.  * <code>CollationKey</code>s returns the relative order of the
  37.  * <code>String</code>s they represent. Using <code>CollationKey</code>s
  38.  * to compare <code>String</code>s is generally faster than using
  39.  * <code>Collator.compare</code>. Thus, when the <code>String</code>s
  40.  * must be compared multiple times, for example when sorting a list
  41.  * of <code>String</code>s. It's more efficient to use <code>CollationKey</code>s.
  42.  *
  43.  * <p>
  44.  * You can not create <code>CollationKey</code>s directly. Rather,
  45.  * generate them by calling <code>Collator.getCollationKey</code>.
  46.  * You can only compare <code>CollationKey</code>s generated from
  47.  * the same <code>Collator</code> object.
  48.  *
  49.  * <p>
  50.  * Generating a <code>CollationKey</code> for a <code>String</code>
  51.  * involves examining the entire <code>String</code>
  52.  * and converting it to series of bits that can be compared bitwise. This
  53.  * allows fast comparisons once the keys are generated. The cost of generating
  54.  * keys is recouped in faster comparisons when <code>String</code>s need
  55.  * to be compared many times. On the other hand, the result of a comparison
  56.  * is often determined by the first couple of characters of each <code>String</code>.
  57.  * <code>Collator.compare</code> examines only as many characters as it needs which
  58.  * allows it to be faster when doing single comparisons.
  59.  * <p>
  60.  * The following example shows how <code>CollationKey</code>s might be used
  61.  * to sort a list of <code>String</code>s.
  62.  * <blockquote>
  63.  * <pre>
  64.  * // Create an array of CollationKeys for the Strings to be sorted.
  65.  * Collator myCollator = Collator.getInstance();
  66.  * CollationKey[] keys = new CollationKey[3];
  67.  * keys[0] = myCollator.getCollationKey("Tom");
  68.  * keys[1] = myCollator.getCollationKey("Dick");
  69.  * keys[2] = myCollator.getCollationKey("Harry");
  70.  * sort( keys );
  71.  * <nsbr>
  72.  * //...
  73.  * <nsbr>
  74.  * // Inside body of sort routine, compare keys this way
  75.  * if( keys[i].compareTo( keys[j] ) > 0 )
  76.  *    // swap keys[i] and keys[j]
  77.  * <nsbr>
  78.  * //...
  79.  * <nsbr>
  80.  * // Finally, when we've returned from sort.
  81.  * System.out.println( keys[0].getSourceString() );
  82.  * System.out.println( keys[1].getSourceString() );
  83.  * System.out.println( keys[2].getSourceString() );
  84.  * </pre>
  85.  * </blockquote>
  86.  *
  87.  * @see          Collator
  88.  * @see          RuleBasedCollator
  89.  * @version      1.4 28 Jan 1997
  90.  * @author       Helena Shih
  91.  */
  92.  
  93. public final class CollationKey {
  94.     /**
  95.      * Compare this CollationKey to the target CollationKey. The collation rules of the
  96.      * Collator object which created these keys are applied. <strong>Note:</strong>
  97.      * CollationKeys created by different Collators can not be compared.
  98.      * @param target target CollationKey
  99.      * @return Returns an integer value. Value is less than zero if this is less
  100.      * than target, value is zero if this and target are equal and value is greater than
  101.      * zero if this is greater than target.
  102.      * @see java.text.Collator#compare
  103.      */
  104.     public int compareTo(CollationKey target)
  105.     {
  106.         int result = key.compareTo(target.key);
  107.         if (result <= Collator.LESS)
  108.             return Collator.LESS;
  109.         else if (result >= Collator.GREATER)
  110.             return Collator.GREATER;
  111.         return Collator.EQUAL;
  112.     }
  113.  
  114.     /**
  115.      * Compare this CollationKey and the target CollationKey for equality.
  116.      * The collation rules of the Collator object which created these keys are applied.
  117.      * <strong>Note:</strong> CollationKeys created by different Collators can not be
  118.      * compared.
  119.      * @param target the CollationKey to compare to.
  120.      * @return Returns true if two objects are equal, false otherwise.
  121.      */
  122.     public boolean equals(Object target) {
  123.         if (this == target) return true;
  124.         if (target == null || !getClass().equals(target.getClass())) {
  125.             return false;
  126.         }
  127.         CollationKey other = (CollationKey)target;
  128.         return key.equals(other.key);
  129.     }
  130.  
  131.     /**
  132.      * Creates a hash code for this CollationKey. The hash value is calculated on the
  133.      * key itself, not the String from which the key was created.  Thus
  134.      * if x and y are CollationKeys, then x.hashCode(x) == y.hashCode() if
  135.      * x.equals(y) is true.  This allows language-sensitive comparison in a hash table.
  136.      * See the CollatinKey class description for an example.
  137.      * @return the hash value based on the string's collation order.
  138.      */
  139.     public int hashCode() {
  140.         return (key.hashCode());
  141.     }
  142.  
  143.  
  144.     /**
  145.      * Returns the String that this CollationKey represents.
  146.      */
  147.     public String getSourceString() {
  148.         return source;
  149.     }
  150.  
  151.  
  152.     /**
  153.      * Converts the CollationKey to a sequence of bits. If two CollationKeys
  154.      * could be legitimately compared, then one could compare the byte arrays
  155.      * for each of those keys to obtain the same result.  Byte arrays are
  156.      * organized most significant byte first.
  157.      */
  158.     public byte[] toByteArray() {
  159.  
  160.         char[] src = key.toCharArray();
  161.         byte[] dest = new byte[ 2*src.length ];
  162.         int j = 0;
  163.         for( int i=0; i<src.length; i++ ) {
  164.             dest[j++] = (byte)(src[i] >>> 8);
  165.             dest[j++] = (byte)(src[i] & 0x00ff);
  166.         }
  167.         return dest;
  168.     }
  169.  
  170.     /**
  171.      * A CollationKey can only be generated by Collator objects.
  172.      */
  173.     CollationKey(String source, String key) {
  174.         this.source = source;
  175.         this.key = key;
  176.     }
  177.  
  178.     private String source = null;
  179.     private String key = null;
  180. }
  181.  
  182.  
  183.  
  184.